package com.sbp.message.service.impl;

import com.dap.dao.DaoSupport;
import com.sbp.message.api.entity.LocalRecordStatus;
import com.sbp.message.api.entity.LocalRecordType;
import com.sbp.message.api.entity.Message;
import com.sbp.message.api.service.MessageStatusAckService;
import com.sbp.message.entity.ProducerMessageRecord;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MessageStatusAckServiceImpl implements MessageStatusAckService {

    private DaoSupport daoService;
    public void setDaoService(DaoSupport daoService) {
        this.daoService = daoService;
    }

    @Override
    public boolean acknowledge(String messageId) {
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        ProducerMessageRecord producerMessageRecord = daoService.selectOne(ProducerMessageRecord.class, "selectByMessageId", map);
        return null != producerMessageRecord;
    }

    @Override
    public void insertAcknowledge(Message message) {
        ProducerMessageRecord producerMessageRecord = new ProducerMessageRecord();
        producerMessageRecord.setMessageId(message.getId());
        producerMessageRecord.setAppId("");
        producerMessageRecord.setDestination(message.getDestination());
        producerMessageRecord.setType(LocalRecordType.LocalRecordType_producer.getValue());
        producerMessageRecord.setStatus(LocalRecordStatus.LocalRecordStatus_succeed.getValue());
        producerMessageRecord.setCreateTime(new Date());
        producerMessageRecord.setRemark(message.getContent());

        daoService.insert(producerMessageRecord);
    }

    @Override
    @Deprecated
    public void insertAcknowledge(String msgId) {
        ProducerMessageRecord producerMessageRecord = new ProducerMessageRecord();
        producerMessageRecord.setMessageId(msgId);
        producerMessageRecord.setAppId("");
        producerMessageRecord.setGroupName("");
        producerMessageRecord.setDestination("");
        producerMessageRecord.setType(LocalRecordType.LocalRecordType_producer.getValue());
        producerMessageRecord.setStatus(LocalRecordStatus.LocalRecordStatus_succeed.getValue());
        producerMessageRecord.setCreateTime(new Date());
        producerMessageRecord.setRemark("");

        daoService.insert(producerMessageRecord);
    }

}
